<?php

/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

class HT_Model_administrator_models_seoauto extends Zend_Db_Table {//ten class fai viet hoa

	protected $_db;

	public function __construct() {
		$this->_name = "seo_auto";
		$this->_db = Zend_Registry::get('dbMain');
		parent::init();
	}
	
	public function getSeoComments($limit){
		$ids = $this->getCommentIds($limit);
		$sql = " SELECT * FROM seo_auto WHERE auto_id IN (".implode(',', $ids).")";
		return $this->_db->fetchAll($sql);
	}
	
		
	private function getCommentIds($limit){
		$ids = array();
		$autoIds = array();
		$sql = " SELECT auto_id FROM seo_auto";
		$idList = $this->_db->fetchAll($sql);
		foreach((array)$idList as $idData){
			$ids[] = $idData['auto_id'];
		}
		
		while(sizeof($autoIds) < $limit){
			$index = rand(0,sizeof($ids)-1);
			if(!in_array($index,$autoIds)){
				$autoIds[] = $index;
			}
		}
		return $autoIds;
	}
	
	public function updateData($data,$seoAutoId){
		$autoContent = trim($data['auto_content']);
		if($autoContent != '' && !$this->_checkExistsSeoauto($autoContent,'update',$seoAutoId)){
			$this->update($data, "auto_id=".(int)$seoAutoId);
			return $seoAutoId;
		}else{
			return "-1";
		}
		
	}
	
	public function addData($data){
		$autoContent = trim($data['auto_content']);
		if($autoContent != '' && !$this->_checkExistsSeoauto($autoContent)){
			$this->insert($data);
			return $this->getMaxId();
		}else{
			return "-1";
		}
	}
	
	private function _checkExistsSeoauto($autoContent,$action="insert",$seoAutoId = null){
		if($action == 'insert'){
			$sql = "SELECT COUNT(auto_id) FROM seo_auto WHERE auto_content = ?";
			return  (int)$this->_db->fetchOne($sql,array($autoContent));
		}else{
			$sql = "SELECT COUNT(auto_id) FROM seo_auto WHERE auto_content = ? AND auto_id <> ?";
			return  (int)$this->_db->fetchOne($sql,array($autoContent,$seoAutoId));
		}
	}
	
	public function getMaxId(){
		$sql = "SELECT MAX(auto_id) FROM seo_auto";
		return  (int)$this->_db->fetchOne($sql);
	}
	public function getSeoauto($seoAutoId,$filter = array()) {
		$sql = " SELECT * FROM seo_auto WHERE auto_id= ".(int)$seoAutoId;
		return $this->_db->fetchRow($sql);
	}
	
	public function getSeoautoBy($url) {
		$sql = " SELECT * FROM seo_auto WHERE auto = ? LIMIT 1 ";
		return $this->_db->fetchRow($sql,array($url));
	}
	
	public function getListSeoauto_nb($filter = array()) {
		$sqlPlus = $this->getListSeoauto_sqlPlus($filter);
		$sql = "SELECT COUNT(auto.auto_id)
		FROM seo_auto auto
		INNER JOIN seo_auto_type otype ON auto.auto_type_id = otype.auto_type_id
		WHERE 1=1 $sqlPlus";
		return $this->_db->fetchOne($sql);
	}
	public function getListSeoauto($start=0,$size = 10,$filter = array()) {
		$sqlPlus = $this->getListSeoauto_sqlPlus($filter);
		$sql = "SELECT auto.*, otype.auto_type_name
		FROM seo_auto auto
		INNER JOIN seo_auto_type otype ON auto.auto_type_id = otype.auto_type_id
		WHERE 1=1 $sqlPlus ORDER BY auto.auto_type_id, auto.auto_content LIMIT $start,$size";
		return $this->_db->fetchAll($sql);
	}
	
	private function getListSeoauto_sqlPlus($filter){
		$sqlPlus = null;
		$keyword = trim(@$filter['keyword']);
		if($keyword){
			$sqlPlus .= " AND (auto.auto_content LIKE '%$keyword%') ";
		}
		return $sqlPlus;
	}
	
}

?>
